﻿namespace WayneGameSolution.Communicate
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using WayneGameSolution.Packs;
    using WayneGameSolution.Strategies;

    public class XmlCodec : Codec
    {
        [CompilerGenerated, DebuggerStepThrough]
        private XElement _Lambda__3(IWorkingPackNode n)
        {
            return this.Node2Xml(n);
        }

        [DebuggerStepThrough, CompilerGenerated]
        private IWorkingPackNode _Lambda__5(XElement chin)
        {
            return this.Xml2Node(chin);
        }

        private XElement Node2Xml(IWorkingPackNode node)
        {
            _Closure__1 VBClosure_ClosureVariable_3C_C = new _Closure__1 {
                VBLocal_node = node
            };
            return new XElement(VBClosure_ClosureVariable_3C_C.VBLocal_node.Name, new object[] { VBClosure_ClosureVariable_3C_C.VBLocal_node.ChildNodes.Select<IWorkingPackNode, XElement>(new Func<IWorkingPackNode, XElement>(this._Lambda__3)), VBClosure_ClosureVariable_3C_C.VBLocal_node.Attritues.Keys.Select<string, XAttribute>(new Func<string, XAttribute>(VBClosure_ClosureVariable_3C_C._Lambda__4)) }) { Value = VBClosure_ClosureVariable_3C_C.VBLocal_node.Value };
        }

        public override ILinkPack ToLinkPack(IWorkingPack workingPack)
        {
            XDocument doc = new XDocument(new object[] { this.Node2Xml(workingPack.RootValueNode) });
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);
            StreamWriter tw = new StreamWriter(ms, Encoding.UTF8);
            bw.Write(0x7fffffff);
            bw.Write(workingPack.Flag);
            doc.Save(tw, SaveOptions.DisableFormatting);
            ms.Seek(0L, SeekOrigin.Begin);
            bw.Write((int) ms.Length);
            return new LinkPack(ms.ToArray());
        }

        public override IWorkingPack ToWorkingPack(ILinkPack linkPack)
        {
            byte[] bytes = linkPack.BinaryData;
            MemoryStream ms = new MemoryStream(bytes, 8, bytes.Count<byte>() - 8);
            ms.Seek(0L, SeekOrigin.Begin);
            StreamReader rd = new StreamReader(ms, Encoding.UTF8);
            XmlTextReader xr = new XmlTextReader(rd);
            XDocument doc = XDocument.Load(xr);
            WorkingPackNode node = (WorkingPackNode) this.Xml2Node(doc.Root);
            IWorkingPack p = (IWorkingPack) GlobalConfig.Current.CreateObject("WayneGameSolution.Packs.pack_" + Enum.GetName(GlobalConfig.Current.PackFlagEnumType, linkPack.Flag), new object[0]);
            p.RootValueNode = node;
            return p;
        }

        private Dictionary<string, string> Xattrs2Dic(IEnumerable<XAttribute> ats)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (XAttribute xa in ats)
            {
                dic.Add(xa.Name.LocalName, xa.Value);
            }
            return dic;
        }

        private IWorkingPackNode Xml2Node(XElement elem)
        {
            return new WorkingPackNode(elem.Name.LocalName, elem.Elements().Select<XElement, IWorkingPackNode>(new Func<XElement, IWorkingPackNode>(this._Lambda__5)), this.Xattrs2Dic(elem.Attributes())) { Value = elem.Value };
        }

        [CompilerGenerated]
        internal class _Closure__1
        {
            public IWorkingPackNode VBLocal_node;

            [DebuggerNonUserCode]
            public _Closure__1()
            {
            }

            [DebuggerNonUserCode]
            public _Closure__1(XmlCodec._Closure__1 other)
            {
                if (other != null)
                {
                    this.VBLocal_node = other.VBLocal_node;
                }
            }

            [CompilerGenerated, DebuggerStepThrough]
            public XAttribute _Lambda__4(string a)
            {
                return new XAttribute(a, this.VBLocal_node.Attritues[a]);
            }
        }
    }
}

